package eclipse.lib.sound;
import javazoom.jl.player.Player;
import eclipse.ErrorManager;
import eclipse.util.*;
public class MpegPlayer extends ThreadedTimer
{
	private Player		mp3Player;	
	private ERL			mp3File;
	private boolean		loop	= false;
	public MpegPlayer()
	{
		initPlayer();
	}
	public MpegPlayer(ERL eFile)
	{
		mp3File = eFile;
		initPlayer();
		loadMP3(eFile);
	}
	public void initPlayer()
	{
		setName("MPEG Player");
		setInterval(10);
		start();
	}
	public void loadMP3(ERL eFile)
	{
		try
		{			
			mp3Player = new Player(eFile.openStream());
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
		}
	}
	public void loopMP3()
	{
		setEnabled(true);
		loop = true;
	}
	public void playMP3()
	{		
		setEnabled(true);
	}
	public void stopMP3()
	{
		mp3Player.close();
	}
	public void pauseMP3()
	{
		setEnabled(false);
	}
	public void resumeMP3()
	{
		setEnabled(true);
	}
	public void tick()
	{
		try
		{
			while(mp3Player.play(1))			
				Sleep(10);
			if(loop)
			{
				loadMP3(mp3File);				
			}
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
		}
	}
}
